import java.util.HashSet;
import java.util.Set;

public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        //"abcabcbb" "bbbbb" "pwwkew"
        String test1 = "abcabcbb";
        String test2 = "bbbbb";
        String test3 = "pwwkew";
        String test4 = " ";
        String test5 ="dvdf";
        LengthOfLongestSubstring lengthOfLongestSubstring = new LengthOfLongestSubstring();
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring(test1));
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring(test2));
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring(test3));
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring(test4));
        System.out.println(lengthOfLongestSubstring.lengthOfLongestSubstring(test5));
    }
    public int lengthOfLongestSubstring(String s) {
        int result = 0;
        int start = 0;
        int startTwo = 0;
        int tempCount =0;
        Set<Character> existSet= new HashSet<>();
        while(start < s.length() && startTwo<s.length()){
           if(!existSet.contains(s.charAt(startTwo))){
               existSet.add(s.charAt(startTwo));
               tempCount ++;
               startTwo ++;
           }else{
               start ++;
               existSet.clear();
               tempCount =0;
               startTwo = start;
           }
            if(tempCount > result){
                result = tempCount;
            }
        }
        return result;
    }
}
